﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace CustomerOrderingSystem
{
    public partial class Exwarehouse : Form
    {
        private string sql = "";
        public Exwarehouse()
        {
            InitializeComponent();
        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }
        private void MakeSqlStr()
        {
            {
                //清空上次的查询字符串
                sql = "";
                if (gnoTextBox.Text.Trim() != string.Empty)
                {
                    sql = " and Gno like '%" + gnoTextBox.Text.Trim() + "%'";
                }
                if (cnoTextBox.Text.Trim() != string.Empty)
                {
                    sql += " and Cno like '%" + cnoTextBox.Text.Trim() + "%'";
                }
            }
        }

        private void Exwarehouse_Load(object sender, EventArgs e)
        {
            stock_OutDataGridView.DataSource = sql_ex.QueryDataTable0();
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            MakeSqlStr();
            stock_OutDataGridView.DataSource = sql_ex.QueryDataTable(sql);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            DialogResult ret = MessageBox.Show("确定要删除记录吗？", "删除", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (ret == DialogResult.Cancel)
            {
                return;
            }
            try
            {

                if (sql_ex.QueryDataTable5(gnoTextBox1.Text, cnoTextBox1.Text) == 1)
                {
                    MessageBox.Show("删除成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                Exwarehouse_Load(null, null);
            }
            finally
            {
                sql_ex.QueryDataTable4();
            }
       
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                int cnt = sql_ex.QueryDataTable1(gnoTextBox1.Text, cnoTextBox1.Text);
                if (cnt == 1)
                {
                    MessageBox.Show("更新成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    sql_ex.QueryDataTable2(outpriceTextBox.Text, outnumTextBox.Text, outdateTextBox.Text, gnoTextBox1.Text, cnoTextBox1.Text);
                }
                // 添加新记录
                else
                {
                    MessageBox.Show("添加成功！", "提示", MessageBoxButtons.OK);
                    sql_ex.QueryDataTable3(gnoTextBox1.Text, cnoTextBox1.Text, outpriceTextBox.Text, outnumTextBox.Text, outdateTextBox.Text);
                }
                Exwarehouse_Load(null, null);
            }
            finally
            {
                sql_ex.QueryDataTable4();
            }
                
        }

        private void textBox7_TextChanged(object sender, EventArgs e)
        {

        }
    }
}